package com.anyisn.anyflickr.modules.PhotoInfo.view
{
	import com.adobe.webapis.flickr.Photo;
	import com.anyisn.anyflickr.modules.PhotoInfo.PhotoInfoModuleFacade;
	import com.anyisn.anyflickr.modules.PhotoInfo.view.component.PhotoInfoWindow;
	import com.anyisn.anyflickr.shell.ShellFacade;
	
	import flash.display.Bitmap;
	import flash.events.Event;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	
	public class PhotoInfoWindowMediator extends Mediator
	{
		public static const NAME:String = 'PhotoInfoWindowMediator';
		
		public function PhotoInfoWindowMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		override public function onRegister() : void {
//			win.addEventListener(PhotoInfoWindow.SHOW_MENU, showMenu);
			win.addEventListener(Event.CLOSE, onWindowClose);
			trace(this.multitonKey);
		}
		
		override public function listNotificationInterests():Array {
			return [
//					ShellFacade.PHOTOS_GET_SIZES_SUCCESS,
					PhotoInfoModuleFacade.GET_PHOTO_INFO_WINDOW,
//					ShellFacade.TO_CLOSE_PHOTO_INFO_WINDOW,
					ShellFacade.CHANGE_PHOTO_IN_PHOTO_INFO_WINDOW
					];
		}
		
		override public function handleNotification(notification:INotification):void {
			switch(notification.getName()) {
				/*case ShellFacade.PHOTOS_GET_SIZES_SUCCESS:
					var sizes:Array = notification.getBody() as Array;
					var psTW:PhotoSizes = new PhotoSizes(sizes);
					psTW.addEventListener(PhotoSizes.DOWNLOAD, downloadPhoto);
					psTW.addEventListener(CloseEvent.CLOSE, closeSizes);
					psTW.x = 5;
					psTW.y = 5;
					win.addChild(psTW);
					break;*/
				case PhotoInfoModuleFacade.GET_PHOTO_INFO_WINDOW:
					sendNotification(PhotoInfoModuleFacade.EXPORT_PHOTO_INFO_WINDOW, win);
					break;
				/*case ShellFacade.TO_CLOSE_PHOTO_INFO_WINDOW:
					var photo:Photo = notification.getBody() as Photo;
					if(win.photo.id == photo.id) {
						win.close();
					}
					break;*/
				case ShellFacade.CHANGE_PHOTO_IN_PHOTO_INFO_WINDOW:
					var array:Array = notification.getBody() as Array;
					var bitmap:Bitmap = array[0] as Bitmap;
					var photo:Photo = array[1] as Photo;
					win.showPhoto(bitmap, photo);
					break;
			}
		}
		
		/*private function showMenu(e:Event):void {
			var menuData:Array = ApplicationConstants.photoMenu;
			var photoMenu:Menu = Menu.createMenu(win, menuData);
			photoMenu.addEventListener(MenuEvent.ITEM_CLICK, clickMenuItem);
			
			var x:Number;
			var y:Number;
			if(win.mouseX > win.width*0.5) {
				x = win.mouseX - photoMenu.width;
			} else {
				x = win.mouseX;
			}
			if(win.mouseY > win.height*0.5) {
				y = win.mouseY - photoMenu.height;
			} else {
				y = win.mouseY;
			}
			photoMenu.show(x, y);
		}*/
		
//		private function clickMenuItem(e:MenuEvent):void {
//			switch(e.item.name) {
//				case ShellFacade.NAVIGATE_TO_PHOTOSTREAM:
//					var rqt0:URLRequest = new URLRequest(Urls.getPhotostreamPage(win.photo.ownerId));
//					navigateToURL(rqt0);
//					break;
//				case ShellFacade.NAVIGATE_TO_PHOTOPAGE:
//					var rqt1:URLRequest = new URLRequest(Urls.getPhotoPage(win.photo.ownerId, win.photo.id));
//					navigateToURL(rqt1);
//					break;
//				case ShellFacade.PERSON_ADD_PREPARE:
//					sendNotification(ShellFacade.PERSON_ADD_PREPARE, win.photo.ownerId);
//					break;
//				case ShellFacade.TILE_PEOPLE_GET_PUBLIC_PHOTOS:
//					sendNotification(ShellFacade.TILE_PEOPLE_GET_PUBLIC_PHOTOS, win.photo.ownerId);
//					break;
//				case ShellFacade.PHOTO_PREPARE_DOWNLOAD:
//					// - not download images that have the no download flag set
//					// - not download images that are All Rights Reserved unless the user has permission from the photo owner
//					/*if(win.photo.license == 0) {
//					Alert.show('All Rights Reserved.', 'Sorry', Alert.OK);
//					} else if(win.photo.canDownload == 0) {
//					Alert.show('This photo have the no download flag set.', 'Sorry', Alert.OK);
//					} else{
//					sendNotification(PhotoInfoModuleFacade.PHOTOS_GET_SIZES, win.photo);
//					}*/
//					sendNotification(PhotoInfoModuleFacade.PHOTOS_GET_SIZES, win.photo);
//					break;
//				case 'back':
//					break;
//				default:
//					sendNotification(e.item.name, win.photo);
//			}
//		}
		
/*		private function downloadPhoto(e:DownloadEvent):void {
			sendNotification(ShellFacade.PHOTO_DOWNLOAD, e.source);
			var psTW:PhotoSizes = e.target as PhotoSizes;
			win.removeChild(psTW);
		}
		
		private function closeSizes(e:CloseEvent):void {
			var psTW:PhotoSizes = e.target as PhotoSizes;
			win.removeChild(psTW);
		}*/
		
		/**
		 * Handle window close.
		 * <P>
		 * Remove all references to the window that exist inside 
		 * the module, then unregister this Mediator.</P>
		 * <P>
		 * Note that the main app has a listener on the component
		 * as well, which it will use to remove it from the view 
		 * hierarchy and then will remove all refs in the app 
		 * itself.</P> 
		 * <P>
		 * After that the module instance and all its contents 
		 * should be garbage collected.</P>
		 */
		private function onWindowClose(e:Event):void {
			win.removeEventListener(Event.CLOSE, onWindowClose);
			setViewComponent(null);
			facade.removeMediator(this.getMediatorName());
		}
		
		private function get win():PhotoInfoWindow {
			return viewComponent as PhotoInfoWindow;
		}
	}
}